<template>
  <div class="datails">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录账号设置">
          <el-form label-width="80px" :model="form" :rules="rules" ref="form">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="update">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" lazy>
          <component
            :is="'UserInfo'"
            :userInfo="initUser"
            @updataUserinfo="updataUserinfo"
          ></component>
          <!-- <UserInfo :userInfo="initUser"></UserInfo> -->
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <component :is="'jobInfo'"></component>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { sysUser, sysUserPut } from "@/api/user.js";
export default {
  components: {
    UserInfo: () => import("./userInfo.vue"),
    JobInfo: () => import("./jobInfo.vue"),
  },
  data() {
    return {
      id: this.$route.params.id,
      user: "",
      initUser: {},
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { require: true, message: "必填", trigger: "change" },
          { min: 2, max: 4, message: "请输入2-4位姓名", trigger: "change" },
        ],
        password: [{ require: true, message: "必填", trigger: "change" }],
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取员工基本信息
    async getData() {
      const res = await sysUser(this.id);
      res.data.formOfEmployment = res.data.formOfEmployment || 1;
      this.user = res.data;
      this.initUser = JSON.parse(JSON.stringify(res.data));
      this.form.username = res.data.username;
    },
    update() {
      this.$refs.form.validate(async (result) => {
        if (result) {
          await sysUserPut({ ...this.user, ...this.form });
          this.$message.success("更新成功");
          this.initUser = { ...this.initUser, ...this.form };
          this.$router.push("/employees");
        }
      });
    },
    updataUserinfo() {
      this.user = JSON.parse(JSON.stringify(this.initUser));
      this.form.username = this.user.username;
    },
  },
};
</script>

<style lang="scss" scoped>
.details {
  padding: 15px;
}
</style>
